package com.yj.web.WeChat.handler;

import com.yj.web.WeChat.Message.WxXmlMessage;
import com.yj.web.WeChat.Message.WxXmlOutMessage;
import com.yj.web.WeChat.Message.WxXmlOutNewsMessage;
import com.yj.web.WeChat.api.WxConfigStorage;
import com.yj.web.WeChat.api.WxService;
import com.yj.web.WeChat.service.WxCoreService;
import com.yj.web.dao.mapper.IntroducerMapper;
import com.yj.web.dao.po.admin.Introducer;
import com.yj.web.util.Session.WxSessionManager;
import com.yj.web.util.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by yanlinyang on 2017/9/11.
 */
@Component
public class ScanHandler extends AbstractHandler {
    @Autowired
    protected WxConfigStorage configStorage;
    @Autowired
    protected WxService wxService;
    @Autowired
    protected WxCoreService coreService;
    @Autowired
    private IntroducerMapper introducerMapper;

    @Override
    public WxXmlOutMessage handle(WxXmlMessage wxMessage, Map<String, Object> context, WxService wxService, WxSessionManager sessionManager) throws WxErrorException {
        String introducerId = wxMessage.getEventKey();
        introducerId = introducerId.replaceAll("qrscene_","");
        Introducer introducer = null;
        final String phone;
        if(!introducerId.startsWith("tel")){
            introducer = introducerMapper.findIntroducerById(Integer.parseInt(introducerId));
            phone = introducer.getPhone();
        } else {
            phone = introducerId.replaceAll("tel","");
        }

        ExecutorService service = Executors.newFixedThreadPool(1);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                String openId = wxMessage.getFromUser();
                Set<String> openIds = introducerMapper.getAllOpenIds();
                if (openIds.contains(openId)) {
                    introducerMapper.updateFans(openId, phone);
                } else {
                    introducerMapper.insertFans(openId, phone);
                }
            }
        });
        service.submit(thread);

        WxXmlOutNewsMessage.Item item = new WxXmlOutNewsMessage.Item();
        item.setDescription("");
        item.setTitle("用医诺电子借条，向朋友借钱看病");
        item.setPicUrl("http://mmbiz.qpic.cn/mmbiz_jpg/UfLVmicNJTDibW4PIaMrlk5nbtcu7iaibUiaTytSqwKfTfibFvicekI8TP13fI59LorG3iaGTKmNEBKWicVeia8rlHf3SxDw/0");
        item.setUrl("http://m.yinuochoukuan.com/register?beInviteCode="+phone);

        WxXmlOutNewsMessage m
                = WxXmlOutMessage.NEWS()
                .addArticle(item)
                .fromUser(wxMessage.getToUser())
                .toUser(wxMessage.getFromUser())
                .build();
        logger.info("scanHandler" + m.getMsgType());
        return m;


//        WxXmlOutTextMessage m
//                = WxXmlOutMessage.TEXT()
//                .content("尊敬的" + wxUser.getNickname() + "，您好！")
//                .fromUser(wxMessage.getToUser())
//                .toUser(wxMessage.getFromUser())
//                .build();
//        logger.info("subscribeMessageHandler" + m.getContent());
//        return m;
    }
}
